package com.czxy.enterprise.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.domain.TbRecruitmentInfo;
import com.czxy.domain.TbUser;
import com.czxy.domain.TbUserLog;
import com.czxy.enterprise.feign.UserFeign;
import com.czxy.enterprise.service.RecruitmentInfoService;
import com.czxy.enterprise.service.TbUserLogService;
import com.czxy.findjob.vo.BaseResult;
import org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户及日志 前端控制器
 * </p>
 *
 * @author xuewen
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/userLog")
public class TbUserLogController {
    @Resource
    private TbUserLogService tbUserLogService;
    @Resource
    private RecruitmentInfoService recruitmentInfoService;
    @Resource
    private UserFeign userFeign;
    @PostMapping("/save")
    public BaseResult save(@RequestBody TbUserLog tbUserLog){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",tbUserLog.getUserId());
        queryWrapper.eq("info_id",tbUserLog.getInfoId());
        TbUserLog userLog = tbUserLogService.getOne(queryWrapper);
        if (userLog!=null){
            userLog.setLogTime(new Date());
            if (userLog.getScore()+tbUserLog.getScore()>5){
                userLog.setScore(5);
            }else {
                userLog.setScore(userLog.getScore()+tbUserLog.getScore());
            }
            tbUserLogService.updateById(userLog);
        }else {
            tbUserLog.setLogTime(new Date());
            tbUserLogService.save(tbUserLog);
        }
        return BaseResult.ok("报存成功");
    }
    @GetMapping("/recommend/{userId}")
    public BaseResult recommend(@PathVariable("userId") Integer userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("score");
        List<TbUserLog> list = tbUserLogService.list(queryWrapper);
        if (list.size()<=0){
            TbUser tbUser = userFeign.findById(userId).getData();
            List<TbRecruitmentInfo> recommend = recruitmentInfoService.recommend(tbUser.getCandidatesId());
            return BaseResult.ok("推荐成功",recommend);
        }else {
            List<TbRecruitmentInfo> recommend = tbUserLogService.recommend(userId);
            if (recommend!=null){
                return BaseResult.ok("推荐成功",recommend);
            }else {
                TbUser tbUser = userFeign.findById(userId).getData();
                List<TbRecruitmentInfo> recommend1 = recruitmentInfoService.recommend(tbUser.getCandidatesId());
                return BaseResult.ok("推荐成功",recommend1);
            }

        }
    }

}

